#!/usr/bin/env python
# Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import dbus
import dbus.service

from swindle import entity


class Profile(entity.Entity):
  _I_PROFILE = 'org.chromium.flimflam.Profile'

  def __init__(self, bus, name):
    entity.Entity.__init__(self, bus, '/profile/%s' % name)
    self.name = name
    self._services = []

  def spath(self, srv):
    return '%s/%s' % (self._object_path, srv)

  def is_favorite(self):
    return False

  def services(self):
    return dbus.Array([x.path() for x in self._services], variant_level=1)

  def add_service(self, srv):
    self._services.append(srv)
    self.PropertyChanged('Services', self.services())

  def del_service(self, srv):
    self._services.remove(srv)
    self.PropertyChanged('Services', self.services())

  def active(self):
    if not self.services:
      return False
    return reduce(lambda x, y: x or y, [x.active() for x in self._services])

  @dbus.service.method(_I_PROFILE, in_signature='', out_signature='a{sv}')
  def GetProperties(self):
    self.log('GetProperties')
    r = {}
    r['OfflineMode'] = dbus.Boolean(not self.active(), variant_level=1)
    r['Services'] = self.services()
    r['Entries'] = dbus.Array([x.name for x in self.services],
                              variant_level=1)
    return r

  @dbus.service.method(_I_PROFILE, in_signature='sv', out_signature='')
  def SetProperty(self, key, val):
    self.log('SetProperty %s %s', key, val)

  @dbus.service.signal(_I_PROFILE, signature='sv')
  def PropertyChanged(self, key, val):
    self.log('PropertyChanged %s %s', key, val)

